package net.oschina.app.viewpagerfragment.AttentionFansFragment;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import net.oschina.app.R;
import net.oschina.app.adapter.BaseRecyclerViewAdapter;
import net.oschina.app.bean.Friend;
import net.oschina.app.bean.FriendsList;
import net.oschina.app.ui.view.CircleImageView;
import net.oschina.app.util.MyHttpUtils;
import net.oschina.app.util.UIUtils;
import net.oschina.app.viewpagerfragment.MyFragment.BaseItemFragment;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/4/2.
 * ?
 */

public class FansFragment extends BaseItemFragment {
    private XRecyclerView mXRcv;
    @Override
    public void showItemFragmentView() {
        View view = View.inflate(UIUtils.getContext(), R.layout.item_fragment, null);
        mXRcv = (XRecyclerView) view.findViewById(R.id.base_detail_xrv);
        LinearLayoutManager lManager = new LinearLayoutManager(getContext());
        mXRcv.setLayoutManager(lManager);
        changeView(view);
    }

    @Override
    public void loadDates() {
        MyHttpUtils load = new MyHttpUtils();
        String url = "http://10.0.2.2:8080/oschina/list/friends_list0/page0.xml";
        load.getDate(url,FriendsList.class,new MyHttpUtils.OngetDatesResult(){
            private List<Friend> mList;
            @Override
            public void Onsucessful(Object bean) {
                FriendsList friendsList = (FriendsList) bean;
                mList = friendsList.getList();
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        FansAdapter adapter = new FansAdapter(getContext(), mList);
                        mXRcv.setAdapter(adapter);
                    }
                });
            }

            @Override
            public void Onfail() {

            }
        });
    }

    class FansHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.user_face)
        CircleImageView mUserFace;
        @BindView(R.id.user_name)
        TextView mUserName;
        @BindView(R.id.user_address)
        TextView mUserAddress;
        @BindView(R.id.user_work)
        TextView mUserWork;
        @BindView(R.id.user_sex)
        ImageView mUserSex;

        public FansHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public class FansAdapter extends BaseRecyclerViewAdapter{
        List<Friend> list;
        public FansAdapter(Context context, List<Friend> list) {
            super(context);
            this.list = list;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(parent.getContext(), R.layout.user_info_item, null);
            FansHolder holder = new FansHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            super.onBindViewHolder(holder, position);
            FansHolder holder1 = (FansHolder) holder;

            Friend friend = list.get(position);
            holder1.mUserName.setText(friend.getName());
            if(friend.getGender() == 1 ){
                holder1.mUserSex.setImageResource(R.drawable.userinfo_icon_male);
            }else {
                holder1.mUserSex.setImageResource(R.drawable.userinfo_icon_female);
            }
            holder1.mUserAddress.setText(friend.getFrom());
            holder1.mUserWork.setText(friend.getExpertise());
            Glide.with(getContext()).load(friend.getPortrait()).into(holder1.mUserFace);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }
}
